﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Badanin M.V.
 * Дата: 28.06.2013
 * Время: 13:29
 * Проект: РПН Регион 2013
 * Распространяется под лицензией GPL v3
 */
using System;
using System.Collections;
namespace Region
{
  /// <summary>
  /// Description of RegionDate.
  /// </summary>
  //class Region: Nullable<DateTime>{};  
  public class RegionDate
  {
    DateTime m_Value;
    Boolean m_Empty;
    public DateTime DateTime{get{return m_Value;}}
    public DateTime Date{get{return m_Value.Date;}}
    public Int32 Day{get{return m_Value.Day;}}
    public DayOfWeek DayOfWeek{get{return m_Value.DayOfWeek;}}
    public Int32 DayOfYear{get{return m_Value.DayOfYear;}}
    public Int32 Hour{get{return m_Value.Hour;}}
    public DateTimeKind Kind{get{return m_Value.Kind;}}
    public Int32 Millisecond{get{return m_Value.Millisecond;}}
    public Int32 Minute{get{return m_Value.Minute;}}
    public Int32 Month{get{return m_Value.Month;}}
    public Int32 Second{get{return m_Value.Second;}}
    public Int64 Ticks{get{return m_Value.Ticks;}}
    public TimeSpan TimeOfDay{get{return m_Value.TimeOfDay;}}
    public Int32 Year{get{return m_Value.Year;}} 
    public Boolean Empty{get{return m_Empty;}set{m_Empty=value;}}
    public String ToShortDateString(){if (m_Empty) return ""; return m_Value.ToShortDateString();}
    public RegionDate()
    {
       m_Empty = true;
    }

    public RegionDate(int year, int month,int day,int hour,int minute,int second, int millisecond)
    {      
	    m_Value = new DateTime(year,  month, day, hour, minute, second, millisecond);
      m_Empty = false;
    }
	  public RegionDate(DateTime date)
    {      
      m_Value = date;
      m_Empty = false;
    }
    public RegionDate(Int32 RegionDate)
    {
      DateTime d = new DateTime(1970, 1, 1);
      m_Value = d.AddDays((Int32)(RegionDate / (3600 * 24)));
      m_Empty = false;
    }
    public Int32 getRegionDate()
    {
      if (this==null) return 0;
      return (m_Value - new DateTime(1970, 1, 1)).Days * 3600 * 24;
    }
    public static implicit operator DateTime(RegionDate p){return p.m_Value;}
    public static implicit operator DateTime?(RegionDate p) { if (p.Empty) return null; else return p.m_Value; }
    public static implicit operator RegionDate(DateTime p){return new RegionDate(p);}
    public static implicit operator RegionDate(DateTime? p) { if (p == null) return new RegionDate(); else return new RegionDate(p.Value); }
    public static bool operator ==(RegionDate a, RegionDate b)
    {
        
      if (Object.Equals(a,null) && Object.Equals(b,null)) return true;
      if (Object.Equals(a,null)) return false;
      if (Object.Equals(b,null)) return false;
      if (a.Empty == true && b.Empty == true)
          return true;
      return a.DateTime==b.DateTime;
    }
    public static bool operator !=(RegionDate a, RegionDate b)
    {
      if (Object.Equals(a,null) && Object.Equals(b,null)) return false;
      if (Object.Equals(a,null)) return true;
      if (Object.Equals(b,null)) return true;
    	return a.DateTime!=b.DateTime;
    }

    public static bool operator<(RegionDate a, RegionDate b)
    {
    	if (a == null || a.Empty) return false;
    	if (b == null || b.Empty) return false;
    	return a.DateTime<b.DateTime;
    }
    public static bool operator>(RegionDate a, RegionDate b)
    {
    	if (a == null || a.Empty) return false;
    	if (b == null || b.Empty) return false;
    	return a.DateTime>b.DateTime;
    }
    public static bool operator <=(RegionDate a, RegionDate b)
    {
        if (a == null || a.Empty) return false;
        if (b == null || b.Empty) return false;
        return a.DateTime <= b.DateTime;
    }
    public static bool operator >=(RegionDate a, RegionDate b)
    {
        if (a == null || a.Empty) return false;
        if (b == null || b.Empty) return false;
        return a.DateTime >= b.DateTime;
    }
  }
}
